/*
 * Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
 *
 * Licensed under the Mulan PSL v2.
 * You can use this file according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of the License at
 *
 *     http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
 * FOR MORE DETAILS.
 */

#pragma once

#include "highfive/H5File.hpp"

#include "DBManager/IRepository/Input/Design/IDVLREL2Repository.h"
#include "DBManager/Repository/HDF5/Base/MainlyRepositoryBase.h"
SG_DBMANAGER_HDF5_NAMESPACE_OPEN
namespace DVLREL2
{
    struct IDENTITY
    {
        SG::DataStructure::Common::Id   ID        = 0;      ///< Identification number
        char                            TYPE[8]   = { 0 };  ///< Name of a load
        SG::DataStructure::Common::Id   SID       = 0;      ///< Load set identification number
        char                            LNAME[8]  = { 0 };  ///< Load name
        SG::DataStructure::Common::Real LMIN      = 0.0;    ///< Minimum value of the load
        SG::DataStructure::Common::Real LMAX      = 0.0;    ///< Maximum value of the load
        SG::DataStructure::Common::Id   EQID      = 0;      ///< DEQATN entry identification number
        SG::DataStructure::Common::I64  ATT1      = 0;      ///< Attribute of the designed load set
        SG::DataStructure::Common::I64  ATT2      = 0;      ///< Attribute of the designed load set
        SG::DataStructure::Common::I64  ATT3      = 0;      ///< Attribute of the designed load set
        SG::DataStructure::Common::I64  ATT4      = 0;      ///< Attribute of the designed load set
        SG::DataStructure::Common::I64  ATT5      = 0;      ///< Attribute of the designed load set
        SG::DataStructure::Common::Id   DVIDS_POS = 0;      ///< Start position in dataset DVIDS
        SG::DataStructure::Common::Id   DVIDS_LEN = 0;      ///< Number of rows in dataset DVIDS
        SG::DataStructure::Common::I64  LABLS_POS = 0;      ///< Start position in dataset LABLS
        SG::DataStructure::Common::I64  LABLS_LEN = 0;      ///< Number of rows in dataset LABLS
        SG::DataStructure::Common::Id   DOMAIN_ID = 0;      ///<
    };
};  // namespace DVLREL2
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DBManager::HDF5::DVLREL2::IDENTITY> ()
{
    HighFive::CompoundType compoundType = { { "ID", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                            { "TYPE", HighFive::create_datatype<char[8]> (), 1, true },
                                            { "SID", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                            { "LNAME", HighFive::create_datatype<char[8]> (), 1, true },
                                            { "LMIN", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
                                            { "LMAX", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
                                            { "EQID", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                            { "ATT1", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "ATT2", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "ATT3", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "ATT4", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "ATT5", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DVIDS_POS", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                            { "DVIDS_LEN", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                            { "LABLS_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "LABLS_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DOMAIN_ID", HighFive::create_datatype<SG::DataStructure::Common::Id> () } };
    return compoundType;
}

template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::DVLREL2Data::DVIDSData> ()
{
    HighFive::CompoundType ct = { { "DVID", HighFive::create_datatype<SG::DataStructure::Common::Id> () } };
    return ct;
}
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::DVLREL2Data::LABLSData> ()
{
    HighFive::CompoundType ct = { { "LABL", HighFive::create_datatype<char[8]> (), 1, true } };
    return ct;
}
SG_DBMANAGER_HDF5_NAMESPACE_OPEN
class DVLREL2Repository : public IDVLREL2Repository, public SG::DBManager::HDF5::MainlyRepositoryBase<SG::DataStructure::FEM::DVLREL2Data>
{
    using MainStruct     = SG::DataStructure::FEM::DVLREL2Data;
    using SubStructDVIDS = SG::DataStructure::FEM::DVLREL2Data::DVIDSData;
    using SubStructLABLS = SG::DataStructure::FEM::DVLREL2Data::LABLSData;

    using NasMainStruct = SG::DBManager::HDF5::DVLREL2::IDENTITY;

  public:
    explicit DVLREL2Repository (HDF5Database& database);

    enum DATASET : std::uint8_t
    {
        MainDataset,
        IndexDataset,
        SubDataset_DVIDS,
        SubDataset_LABLS
    };

    /**
     * @brief   : 初始化表数据
     * @author  : wuxingchuang
     */
    void InitialDatasetInfo () override;

    /**
     * @brief   : 将缓存数据转储到文件内
     */
    void DumpWriteCahce () override;

    /**
     * @brief   : 加载文件数据至缓存
     * @param   : loadIndex
     */
    std::vector<DataType>* LoadReadCache (std::size_t loadIndex) override;

    /**
     * @brief   : 对数据进行排序
     */
    void SortData () override;

    /**
     * @brief   : 获取所有数据
     */
    std::vector<MainStruct> GetAllData () override;

    /**
     * @brief   : 将自己的数据转存为nastran的数据结构
     */
    void OwnToNastran (std::vector<MainStruct>&          own,
                       std::vector<NasMainStruct>& _OUT  IDENTITYData,
                       std::vector<SubStructDVIDS>& _OUT subDataDVIDS,
                       std::vector<SubStructLABLS>& _OUT subDataLABLS,

                       bool isSort = false);

    /**
     * @brief   : 将NASTRAN的数据转存为自己的数据结构
     * @param   : IDENTITYData
     * @param   : subData
     * @param   : own
     */
    void NastranToOwn (std::vector<NasMainStruct>&   IDENTITYData,
                       std::vector<SubStructDVIDS>&  subDataDVIDS,
                       std::vector<SubStructLABLS>&  subDataLABLS,
                       std::vector<MainStruct>& _OUT own);
};
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE
